package com.jiangnan.view;

import com.jiangnan.components.CustomSwitchButton;
import com.jiangnan.constants.MainButton;
import com.jiangnan.utils.SwingUtil;

import javax.swing.*;
import java.awt.*;

public class HomeView extends BaseFrame {
    private JPanel homePanel;
    private JPanel settingsPanel;
    private JPanel aboutPanel;

    public HomeView() {
        setTitle("AI face 管理系统");
        setIconImage(new ImageIcon(this.getClass().getResource("/image/icon.png")).getImage());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1600, 1200);
        setLocationRelativeTo(null);

        // 创建选项卡面板
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBackground(Color.WHITE);
        tabbedPane.setForeground(Color.BLUE);
        tabbedPane.setFont(new Font("宋体", Font.PLAIN, 20));
        // 主页
        homePanel = createPanelWithBackground("/image/bg6.jpg");
        addButtons(homePanel);
        tabbedPane.addTab("主页", homePanel);

        // 设置
        settingsPanel = createSettingsPanel();
        tabbedPane.addTab("设置", settingsPanel);

        // 关于
        aboutPanel = createAboutPanel();
        tabbedPane.addTab("关于", aboutPanel);

        add(tabbedPane);
        setVisible(true);
    }

    private JPanel createAboutPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel titleLabel = new JLabel("公司名称");
        titleLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        panel.add(titleLabel);

        JLabel contactLabel = new JLabel("联系方式: 123-456-7890");
        contactLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        panel.add(contactLabel);

        JLabel copyrightLabel = new JLabel("© 2023 公司名称");
        copyrightLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        panel.add(Box.createVerticalStrut(10));
        panel.add(new JSeparator(JSeparator.HORIZONTAL));
        panel.add(Box.createVerticalStrut(10));
        panel.add(copyrightLabel);

        JLabel versionLabel = new JLabel("版本号: 1.0");
        versionLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        panel.add(versionLabel);
        return panel;
    }

    private JPanel createSettingsPanel() {
        JPanel panel = createPanelWithBackground("/image/bg2.png");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(5, 5, 5, 5);

        // 添加通用设置标题
        panel.add(SwingUtil.getTitleJLabel("通用设置"), gbc);
        // 添加分隔线
        gbc.gridy++;
        gbc.gridwidth = 2; // 设置跨越两列
        panel.add(SwingUtil.getJSeparator(), gbc);
        gbc.gridwidth = 1; // 恢复默认值

        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项1"), gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项2"),gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        // 添加文本输入框
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("文本1"),gbc);
        gbc.gridx = 1;
        panel.add(new JTextField(15),gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("文本2"),gbc);
        gbc.gridx = 1;
        panel.add(new JTextField(15),gbc);

        // 添加选择框
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选择框1"),gbc);
        gbc.gridx = 1;
        panel.add(new JComboBox<>(new String[]{"选项A", "选项B", "选项C"}),gbc);


        // 添加外观设置标题
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getTitleJLabel("外观设置"), gbc);
        // 添加分隔线
        gbc.gridy++;
        gbc.gridwidth = 2; // 设置跨越两列
        panel.add(SwingUtil.getJSeparator(), gbc);
        gbc.gridwidth = 1; // 恢复默认值

        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项1"), gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项2"),gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        // 添加选择框
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选择框1"),gbc);
        gbc.gridx = 1;
        panel.add(new JComboBox<>(new String[]{"选项A", "选项B", "选项C"}),gbc);

        // 添加系统设置标题
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getTitleJLabel("系统设置"), gbc);
        // 添加分隔线
        gbc.gridy++;
        gbc.gridwidth = 2; // 设置跨越两列
        panel.add(SwingUtil.getJSeparator(), gbc);
        gbc.gridwidth = 1; // 恢复默认值

        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项1"), gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选项2"),gbc);
        gbc.gridx = 1;
        panel.add(new CustomSwitchButton(),gbc);

        // 添加选择框
        gbc.gridx = 0;
        gbc.gridy++;
        panel.add(SwingUtil.getJLabel("选择框1"),gbc);
        gbc.gridx = 1;
        panel.add(new JComboBox<>(new String[]{"选项A", "选项B", "选项C"}),gbc);

        return panel;
    }

    private JPanel createPanelWithBackground(String imagePath) {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                try {
                    Image image = new ImageIcon(this.getClass().getResource(imagePath)).getImage();
                    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        panel.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
        return panel;
    }

    private void addButtons(JPanel panel) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE; // 不让组件填充区域
        gbc.insets = new Insets(5, 5, 5, 5); // 设置按钮之间的间距

        // 第一个按钮，位于第一行第一列
        JButton button1 = SwingUtil.createButton(MainButton.ATTENDANCE_MANAGEMENT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(button1, gbc);

        // 第二个按钮，位于第一行第二列
        JButton button2 = SwingUtil.createButton(MainButton.SIGN_IN);
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel.add(button2, gbc);

        // 第三个按钮，位于第二行第一列
        JButton button3 = SwingUtil.createButton(MainButton.CLASS_MANAGEMENT);
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(button3, gbc);

        // 第四个按钮，位于第二行第二列
        JButton button4 = SwingUtil.createButton(MainButton.FACE_REC);
        gbc.gridx = 1;
        gbc.gridy = 1;
        panel.add(button4, gbc);

        // 第三个按钮，位于第一行第三列
        JButton button5 = SwingUtil.createButton(MainButton.DATABASE);
        gbc.gridx = 2;
        gbc.gridy = 0;
        panel.add(button5, gbc);

        // 第四个按钮，位于第二行第三列
        JButton button6 = SwingUtil.createButton(MainButton.MENU);
        gbc.gridx = 2;
        gbc.gridy = 1;
        panel.add(button6, gbc);

    }

}
